package com.exam.controller;

import com.exam.entity.PaperResult;
import com.exam.entity.Resource;
import com.exam.entity.common.ResponseResult;
import com.exam.service.ResourceService;
import com.exam.util.JsonUtils;
import com.exam.util.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


@Controller
public class ResourceController {

    @Autowired
    @Qualifier("resourceServiceImpl")
    private ResourceService resourceService;
    ResponseResult responseResult=new ResponseResult();

    @RequestMapping("/resource")
    public String resource(){
        return "views/manage/resource";
    }

    @RequestMapping("/upload_resource")
    @ResponseBody
    public String upload_resource(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        //获取要上传的目标文件夹绝对路径
        ServletContext context = request.getServletContext();
        String realPath = context.getRealPath("/static/upload_resource");

        String fileName = UUID.randomUUID().toString().replace("-", "").substring(0, 15) + "_file_" + file.getOriginalFilename();
        try {
            //上传文件至指定位置
            file.transferTo(new File(realPath + "/" + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        String r_src="static/upload_resource/" + fileName;
        Resource resource=new Resource();
        resource.setR_name(file.getOriginalFilename());
        resource.setR_src(r_src);
        resource.setR_creat_time(TimeUtils.getTime());
        resource.setR_state("不开放");
        int i = resourceService.save_upload_resource(resource);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("上传成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("上传失败");
        }
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/resource_library")
    public String resource_library(){
        return "views/manage/resource_library";
    }


    @RequestMapping("/get_all_resource")
    @ResponseBody
    public String get_all_resource(){
        List<Resource> all_resource = resourceService.get_all_resource();
        responseResult.setCode(0);
        responseResult.setMessage("成功");
        responseResult.setResult(all_resource);
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/update_resource_state_by_r_id")
    @ResponseBody
    public String update_resource_state_by_r_id(Resource resource){
        int i=resourceService.update_resource_state_by_r_id(resource);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("更新成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("失败");
        }
        return JsonUtils.getJson(responseResult);
    }

    @RequestMapping("/del_resource_state_by_r_id")
    @ResponseBody
    public String del_resource_state_by_r_id(int r_id){
        int i=resourceService.del_resource_state_by_r_id(r_id);
        if (i==1){
            responseResult.setCode(200);
            responseResult.setMessage("删除成功");
        }else {
            responseResult.setCode(0);
            responseResult.setMessage("删除失败");
        }
        return JsonUtils.getJson(responseResult);
    }


    @RequestMapping("/resources_library")
    public String resources_library(){
        return "views/user/resource_library";
    }


    @RequestMapping("/get_resource_by_r_state")
    @ResponseBody
    public String get_resource_by_r_state(){
        List<Resource> all_resource = resourceService.get_resource_by_r_state();
        responseResult.setCode(0);
        responseResult.setMessage("成功");
        responseResult.setResult(all_resource);
        return JsonUtils.getJson(responseResult);
    }
}

